package com.rabbit.mq;

import com.rabbit.config.MqConsumerConfig;
import com.rabbit.entity.Device;
import com.rabbit.entity.Room;
import com.rabbit.entity.User;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * @author yuhuan
 * @date 2025/4/21
 */
@Service
public class SyncConsumer {

    /**
     * 从配置中获取队列名称{@link MqConsumerConfig#userQueue()}
     */
    @RabbitListener(queues = "#{userQueue.name}", errorHandler = "unifiedExceptionHandler")
    public void receiveUser(User user) {
        // 如果处理成功，Spring AMQP 会自动确认消息
        System.out.println(String.format("接收到用户消息[%s]，正在处理：%s", user.getName(), user));
    }

    @RabbitListener(queues = "#{roomQueue.name}", errorHandler = "unifiedExceptionHandler")
    public void receiveRoom(Room room) {
        System.out.println(String.format("接收到房间消息[%s]，正在处理：%s", room.getName(), room));
    }

    @RabbitListener(queues = "#{deviceQueue.name}", errorHandler = "unifiedExceptionHandler")
    public void receiveDevice(Device device) {
        System.out.println(String.format("接收到设备消息[%s]，正在处理：%s", device.getName(), device));
        // 模拟处理消息时发生错误
        throw new RuntimeException("主动抛出异常");
    }
}
